package com.starview.star.compiler.canonical.compile;

import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodNode;

import com.starview.platform.data.type.Location;
import com.starview.star.code.repository.CodeCatalog;
import com.starview.star.compiler.ErrorReport;
import com.starview.star.compiler.cafe.compile.CafeDictionary;
import com.starview.star.compiler.cafe.compile.CodeContext;
import com.starview.star.compiler.cafe.compile.HWM;
import com.starview.star.compiler.cafe.compile.LiveMap;

public class CompileContext
{
  protected final HWM hwm;
  protected final ErrorReport errors;
  protected final ClassNode klass;
  protected final MethodNode mtd;
  protected final Continue cont;
  protected final CodeCatalog bldCat;
  protected final CafeDictionary dict;
  protected final CodeContext ccxt;
  private final LiveMap localMap;
  protected final FrameState frame;

  public CompileContext(HWM hwm, ErrorReport errors, ClassNode klass, MethodNode mtd, Continue cont,
      CafeDictionary dict, CodeCatalog bldCat, LiveMap localMap, FrameState frame)
  {
    this.hwm = hwm;
    this.errors = errors;
    this.klass = klass;
    this.mtd = mtd;
    this.cont = cont;
    this.dict = dict;
    this.bldCat = bldCat;
    this.ccxt = null;
    this.localMap = localMap;
    this.frame = frame;
  }

  public int bumpHwm(int amnt)
  {
    return hwm.bump(amnt);
  }

  public void resetHwm(int mark)
  {
    hwm.reset(mark);
  }

  public LiveMap getLocalMap()
  {
    return localMap;
  }

  public FrameState getFrame()
  {
    return frame;
  }

  public void reportError(String msg, Location... locs)
  {
    errors.reportError(msg, locs);
  }

  public ErrorReport getErrors()
  {
    return errors;
  }

  public HWM getHwm()
  {
    return hwm;
  }

  public MethodNode getMtd()
  {
    return mtd;
  }

  public InsnList getIns()
  {
    return mtd.instructions;
  }

  public CodeCatalog getBldCat()
  {
    return bldCat;
  }

  public Continue getCont()
  {
    return cont;
  }

  public CafeDictionary getDict()
  {
    return dict;
  }

  public FrameState cont(FrameState src, Location loc)
  {
    return cont.cont(src, loc, this);
  }

  public CompileContext fork(Continue cont)
  {
    return new CompileContext(hwm, errors, klass, mtd, cont, dict, bldCat, localMap, frame);
  }
  
  public CompileContext fork(Continue cont,FrameState frame)
  {
    return new CompileContext(hwm, errors, klass, mtd, cont, dict, bldCat, localMap, frame);
  }

  public CompileContext fork(CafeDictionary dict)
  {
    return new CompileContext(hwm, errors, klass, mtd, cont, dict, bldCat, localMap, frame);
  }
}
